<?php
/**
 * Created by PhpStorm.
 * User: aDaddy
 * Date: 2016/7/25
 * Time: 9:55
 */

namespace App\Model;
use App\Base\IRI;
use App\Entity\AdminMenu;
use App\Base\BaseModel;
use App\Setting\AdminMenuSetting;

class AdminMenuModel extends BaseModel{

    /*
     * 列表基本设置【通用模块必须】
     */
    public function listSettingDefault()
    {
        $settings = new AdminMenuSetting($this->getDoctrine());
        return $settings->listConfig();
    }

    //获取所有分类
    public function getAllAdminMenu(){
        return $this->getRep()->findBy(array("parent"=>null),array("sort"=>"ASC"));
    }

    //获取所有分类并构建分类树
    public function getAllAdminMenuTree()
    {
        $root = $this->getAllAdminMenu();
        $this->createTree($root,$list);
        $data = array();
        $levels = [];
        if (!$list) {
            $list = [];
        }
        /**@var $en AdminMenu */
        foreach ($list as $en) {

            if (!$en->getParent()){
                $level = 0;
            }else{
                $level = $levels[$en->getParent()->getId()] + 1;
            }
            $levels[$en->getId()] = $level;
            $prefix = "";
            if ($level > 0) {
                $prefix .="├";
                for ($i=0;$i<$level;$i++ ) {
                    $prefix .="────";
                }
            }
            $data[] = [
                "level" => $level,
                "data" => $en,
                "pre" => $prefix,
            ];
        }
        return $data;
    }

    function createTree($objectList , &$rst = [] ,$level = 0)
    {
        foreach ($objectList as $parent) {
            $rst[] = $parent;
            if ($sons= $parent->getSons()) {
                $this->createTree($sons,$rst,$level+1);
            }
        }
    }

    //获取第一层分类
    public function getRootAdminMenu()
    {
        $qb = $this->getQueryBuilder();
        $qb->select("AdminMenu")
            ->where($qb->expr()->isNull("AdminMenu.parent"))
            ->from("App:AdminMenu","AdminMenu");
        $qb->orderBy("AdminMenu.sort","ASC");
        return $qb->getQuery()->execute();
    }

    public function getSons($parent)
    {
        $qb = $this->getQueryBuilder();
        $qb->select("AdminMenu")
            ->where("AdminMenu.parent = :parent")
            ->setParameter("parent",$parent)
            ->from("App:AdminMenu","AdminMenu");

        dd($qb->getQuery()->getSQL());
        return $qb->getQuery()->execute();
    }

    public function getSons4Setting($parent=null)
    {

        $data = $this->getAllAdminMenuTree();
        $return = [];
        foreach ($data as $row) {
            if (!$parent) {
                $return[ IRI::createIRIStringFromEntity(AdminMenu::class,$row["data"]->getId())] = $row["pre"] . $row["data"]->getName();
            }else{
                if ($row["data"]->getParent() && $row["data"]->getParent()->getId() == $parent) {
                    $return[ IRI::createIRIStringFromEntity(AdminMenu::class,$row["data"]->getId())] = $row["pre"] . $row["data"]->getName();
                }
            }
        }
        return $return ;
    }

    /*
     * 通用Rep
     */
    public function getRep()
    {
        $settings = new AdminMenuSetting($this->getDoctrine());
        return $this->em->getRepository($settings->bundleName.":".$settings->entityName);
    }


}